package com.airsystem.pos.bengkel.controller;

import javax.accessibility.Accessible;
import javax.swing.JOptionPane;
import javax.swing.text.JTextComponent;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.entity.DetailSparepart;
import com.airsystem.pos.bengkel.interfaces.IMaster;
import com.airsystem.pos.bengkel.interfaces.IServices;
import com.airsystem.pos.bengkel.interfaces.IValidator;
import com.airsystem.pos.bengkel.model.MasterSparepartModel;
import com.airsystem.pos.bengkel.service.SparepartServices;
import com.airsystem.pos.bengkel.util.FormValidator;
import com.airsystem.pos.bengkel.view.dialog.MasterSparepartDialog;
import com.airsystem.pos.bengkel.view.dialog.MasterSparepartTypeDialog;
import com.airsystem.pos.bengkel.view.dialog.MasterVehicleDialog;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterSparepartController extends MasterSparepartModel implements IMaster {
	private static final Logger LOG = Logger.getLogger(MasterSparepartController.class.getSimpleName());

	private IValidator<Accessible> formValidator;

	private IServices<DetailSparepart> service;

	public MasterSparepartController() {
		super();

		service = new SparepartServices();

		formValidator = new FormValidator();
	}

	public void setup() {
		updateButton.setVisible(false);
		deleteButton.setVisible(false);
		idField.setVisible(false);

		jenisField.setEditable(false);
		merkkendaraanField.setEditable(false);
		submerkkendaraanField.setEditable(false);
	}

	public void search() {
		dialogMasterSparepart = new MasterSparepartDialog();

		try {
			if (dialogMasterSparepart.getDetailSparepart() != null) {
				DetailSparepart detailSparepart = dialogMasterSparepart.getDetailSparepart();

				idField.setText(detailSparepart.getJenis_sparepart().getId_s());
				merksparepartField.setText(detailSparepart.getSparepart().getMerk());
				submerksparepartField.setText(detailSparepart.getSub_merk());
				merkkendaraanField.setText(detailSparepart.getDetail_kendaraan().getKendaraan().getMerk());
				submerkkendaraanField.setText(detailSparepart.getDetail_kendaraan().getSub_merk());
				jenisField.setText(detailSparepart.getJenis_sparepart().getNama());
				hargaField.setText(detailSparepart.getHargaSatuan_s());
				stokField.setText(detailSparepart.getStok_s());

				saveButton.setVisible(false);
				updateButton.setVisible(true);
				deleteButton.setVisible(true);
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}

	/**
	 * Button action
	 */
	public void get() {
		switch (choice) {
		case JENIS_KENDARAAN: {
			dialogMasterVehicle = new MasterVehicleDialog();

			try {
				if (dialogMasterVehicle.getSubMerk() != null) {
					merkkendaraanField.setText(dialogMasterVehicle.getMerk());
					submerkkendaraanField.setText(dialogMasterVehicle.getChosenSubMerk());
				}
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
			}

			break;
		}
		case JENIS_SPAREPART: {
			dialogMasterSparepartType = new MasterSparepartTypeDialog();

			try {
				if (dialogMasterSparepartType.getJenisSparepart() != null) {
					idField.setText(dialogMasterSparepartType.getJenisSparepart().getId_s());
					jenisField.setText(dialogMasterSparepartType.getJenisSparepart().getNama());
				}
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
			}
		}
		}
	}

	public void reset() {
		idField.setText("");
		merksparepartField.setText("");
		submerksparepartField.setText("");
		merkkendaraanField.setText("");
		submerkkendaraanField.setText("");
		jenisField.setText("");
		hargaField.setText("");
		stokField.setText("");
	}

	public void save() {
		if (!isFormValid(getComponent())) {
			return;
		}

		try {
			service.saveOrUpdate(getComponentData());

			JOptionPane.showMessageDialog(this, "Simpan data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
			reset();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Simpan data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void update() {
		if (!isFormValid(getComponent())) {
			return;
		}

		try {
			service.saveOrUpdate(getComponentData());

			JOptionPane.showMessageDialog(this, "Ubah data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
			cancel();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Ubah data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void delete() {
		if (!isFormValid(getComponent())) {
			return;
		}

		try {
			service.delete(getComponentData());

			JOptionPane.showMessageDialog(this, "Hapus data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
			cancel();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Hapus data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void cancel() {
		reset();

		try {
			saveButton.setVisible(true);
			updateButton.setVisible(false);
			deleteButton.setVisible(false);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}

	protected boolean isFormValid(JTextComponent... component) {
		try {
			formValidator.isValid(component);

			return true;
		} catch (IllegalArgumentException e) {
			JOptionPane.showMessageDialog(this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
		}

		return false;
	}
}